package ch.dreamweb.readpractice.client.lesetraining;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Lesetraining implements EntryPoint {

	/**
	 * This field gets compiled out when <code>log_level=OFF</code>, or any
	 * <code>log_level</code> higher than <code>DEBUG</code>.
	 */
	private long startTimeMillis;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {
		/*
		 * Use a <code>if (Log.isDebugEnabled()) {...}</code> guard to ensure
		 * that <code>System.currentTimeMillis()</code> is compiled out when
		 * <code>log_level=OFF</code>, or any <code>log_level</code> higher than
		 * <code>DEBUG</code>.
		 */
		if (Log.isDebugEnabled()) {
			startTimeMillis = System.currentTimeMillis();
		}
		/*
		 * No guards necessary. Code will be compiled out when
		 * <code>log_level=OFF</code>
		 */
		Log.debug("This is a 'DEBUG' test message");
		Log.info("This is a 'INFO' test message");
		Log.warn("This is a 'WARN' test message");
		Log.error("This is a 'ERROR' test message");
		Log.fatal("This is a 'FATAL' test message");

		/*
		 * Again, we need a guard here, otherwise <code>log_level=OFF</code>
		 * would still produce the following useless JavaScript: <pre> var
		 * durationSeconds, endTimeMillis; endTimeMillis =
		 * currentTimeMillis_0(); durationSeconds = (endTimeMillis -
		 * this$static.startTimeMillis) / 1000.0; </pre>
		 */
		if (Log.isDebugEnabled()) {
			long endTimeMillis = System.currentTimeMillis();
			float durationSeconds = (endTimeMillis - startTimeMillis) / 1000F;
			Log.debug("Duration: " + durationSeconds + " seconds");
		}

		RootPanel.get("loading").setVisible(true);
		loadLesetraining();

	}

	private void loadLesetraining() {
		TrainingServiceAsync rpcService = GWT.create(TrainingService.class);
		EventBus eventBus = new SimpleEventBus();
		AppController appViewer = new AppController(rpcService, eventBus,
				RootPanel.get("loading"));
		appViewer.go(RootPanel.get("main"));
	}

}
